package com.vincent.kdp.ui.fragment.presenter;

import android.content.Intent;

import com.vincent.baseapp.base.BasePresenter;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.Constants;
import com.vincent.kdp.ui.bean.response.WorkerLoginOutput;
import com.vincent.kdp.ui.fragment.contract.CenterContract;
import com.vincent.kdp.ui.fragment.repository.CenterRepository;

public class CenterPresenter extends BasePresenter<CenterContract.View> implements CenterContract.Presenter {

    public CenterRepository mRepository;

    public CenterPresenter(){
        mRepository = new CenterRepository();
    }

    @Override
    public void initView(Intent intent) {
        mView.setLoginViewVisible(false);
    }

    @Override
    public void login() {
//        mView.setLoginViewVisible(false);
        login(mView.getAccount());
    }

    public void login(String phone) {
        if (phone == null || phone.trim().equals("")) {
            mView.showToast("请输入登录账号");
            return;
        }

        //mView.showLoadDialog();
        mRepository.login(phone, new RequestCallBack<WorkerLoginOutput>() {
            @Override
            public void onSuccess(WorkerLoginOutput object) {
                // TODO: 2016/10/23 保存登录之后的员工信息
                if (mView != null) {
                    SPUtil.put(Constants.SPKeys.DEPART_ID, object.getData().getDepartId());
                    SPUtil.put(Constants.SPKeys.IS_LOGIN, true);
                    mView.setLoginViewVisible(false);

                    mView.dismissLoadDialog();
                }
            }

            @Override
            public void onFalse(String msg) {
                if (mView != null) {
                    mView.dismissLoadDialog();
                    mView.showToast(msg);}}});}}
